package com.bj58.migrate.export;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

import com.bj58.migrate.ScfProxy;
import com.bj58.migrate.helper.FileUtil;
import com.bj58.migrate.helper.dao.CommonDAOImpl;
import com.bj58.migrate.helper.dao.ICommonDAO;
import com.bj58.migrate.model.AvgSalaryModel;
import com.bj58.sfft.cmc.entity.DispCategory;
import com.bj58.sfft.imc.contract.IInfoService;
import com.bj58.sfft.imc.entity.Info;
import com.bj58.spat.scf.client.proxy.builder.ProxyFactory;


public class ExportSalaryData{
	
	static{
		com.bj58.spat.scf.client.SCFInit.init( "/opt/apps/app/avgsalary/conf/scf.config" );
	}
	
	public static Integer[] cityids = { 1, 2, 3, 4, 172, 158, 483, 102, 79, 265, 122, 910, 5, 93, 606, 413 };
	public static String[] startTime = { "20141001", "20141101", "20141201", "20150101", "20150201", "20150301", "20150401" };


	private IInfoService init(){

		com.bj58.spat.scf.client.SCFInit.init( "D:\\opt\\wf\\postweb\\scf.config" );
		return ProxyFactory.create( IInfoService.class, "tcp://imc/InfoProviderWithIndex" );
	}

	private String repair(){

		IInfoService service = init();
		try{
			Info info = service.GetInfo( 21701056136995L, "*" );
			if( info != null ){
				info.setSortID( 483055199L );
				service.UpdateInfo( info );
				return "ok";
			}
		}
		catch( Exception e ){
			e.printStackTrace();
		}
		return "fail";
	}

	public static void main( String[] args ){

		// System.out.println( new ExportSalaryData().repair() );

		// File file = new File("/opt/apps/app/avgsalary/hehe.txt");
		// FileUtil.writeAppendFile( "/opt/apps/app/avgsalary/hehe.txt", "\nhehe", "UTF-8" );

		new ExportSalaryData().test();
	}

	public void test(){

		ICommonDAO commonDao = new CommonDAOImpl();
		
		Map< String, List< String > > dataMap = new HashMap< String, List< String > >();

		for( int i = 0; i < cityids.length; i++ ){
			try{
				List< DispCategory > list = ScfProxy.cmc_DispCategory().GetDispCategoryByParentID( 9224 );
				if( list != null ){
					for( DispCategory dispCategory : list ){
						if( dispCategory != null && dispCategory.getIsVisible() ){
							if( dispCategory.getDepth() == 1 && dispCategory.getIsVisible() && dispCategory.getType() != 2 ){
								List< DispCategory > children = ScfProxy.cmc_DispCategory().GetDispCategoryByParentID( dispCategory.getDispCategoryID() );
								if( children != null ){
									for( DispCategory child : children ){
										if( child != null && child.getIsVisible() && child.getType() != 2 ){
											for( int j = 0; j < startTime.length; j++ ){
												String sql = "select * from t_avgsalary2015 where cateid=" + child.getInfoCateidList() + "  and localid=" + cityids[ i ] + " and startdate='" + startTime[ j ] + "'";
												System.out.println( sql );
												List< AvgSalaryModel > resultList = commonDao.getListBySql( AvgSalaryModel.class, sql );
												// int count = commonDao.getCountBySql(
												// "select count(1) from t_avgsalary2015 where cateid=" + cityids[ i ] +
												// " and localid=" + child.getInfoCateidList() + " and startdate='" +
												// startTime[ j ] + "'" );
												List< String > arrayList = dataMap.get( startTime[ j ] );
												if( arrayList == null ){
													List< String > newList = new ArrayList< String >();
													dataMap.put( startTime[ j ], newList );
													arrayList = newList;
												}
												if( resultList != null && resultList.size() > 0 ){
													AvgSalaryModel avg = resultList.get( 0 );
													arrayList.add( "time:" + startTime[ j ] + " cateid:" + child.getInfoCateidList() + " localid:" + cityids[ i ] + " count：" + avg.getInfocount() );
												}
												else{
													System.out.println( "resultList:" + resultList + "-------------------------" );
												}
											}
										}
									}
								}
							}
						}
					}
				}
			}
			catch( Exception e ){
				e.printStackTrace();
			}
		}
		
		Set< Entry< String, List< String >>> set = dataMap.entrySet();
		
		for( Iterator< Entry< String, List< String >> > iter = set.iterator(); iter.hasNext(); ){
			Entry< String, List< String >> entity = (Entry< String, List< String >>)iter.next();
			List< String > arrayList = entity.getValue();
			for(String list : arrayList){
				FileUtil.writeAppendFile( "/opt/apps/app/avgsalary/log/" + entity.getKey() + ".txt", list + "\n", "UTF-8" );
				System.out.println( list );
				System.out.println( entity.getKey() );
			}
		}
	}
}
